package com.schoolCommunity.util;

import com.alibaba.fastjson.JSON;
import com.schoolCommunity.entity.ConfessionWall;
import com.schoolCommunity.entity.Member;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;

//同步mysql插入elasticsearch
@Component
public class InsertES {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    public void InsertConfessionWall(ConfessionWall confessionWall) throws IOException {

        //创建请求
        IndexRequest request = new IndexRequest("confession_wall");

        //规则  PUT /yth_index/_doc/1
        //不设置id则产生随机id
        request.id(confessionWall.getId().toString());
        //设置超时
        request.timeout(TimeValue.timeValueSeconds(1));

        //将数据放入请求json ,需要引入fastJson
        request.source(JSON.toJSONString(confessionWall), XContentType.JSON);

        //客户端发送请求
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);

        System.out.println(response.toString());
        System.out.println("表白墙插入elasticsearch状态" + response.status());  //对应命令返回的状态 CREATED


    }


}
